package com.harassment.interception.ui.fragments.setting;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;

import com.harassment.interception.R;
import com.harassment.interception.base.HIApplication;

public class SettingFragment extends PreferenceFragmentCompat {
    private static final String TAG = "SettingFragment";
    public static final String HARASSMENT_INTERCEPTION_SWITCH = "harassment_interception_switch";
    private ActionBar mActionBar;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.fragment_setting, rootKey);
        Log.d(TAG, "onCreatePreferences: ");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
        if (isHidden()) {
            return;
        }
        initActionBar();
    }

    private void initActionBar() {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        AppCompatActivity appCompatActivity = null;
        if (activity instanceof AppCompatActivity) {
            appCompatActivity = (AppCompatActivity) activity;
        } else {
            return;
        }

        if (mActionBar == null) {
            mActionBar = appCompatActivity.getSupportActionBar();
        }

        mActionBar.setTitle(R.string.setting);
//        mActionBar.setIcon(R.drawable.ic_settings_white_24dp);
    }

    private void rollbackAction() {
        if (mActionBar == null) {
            return;
        }
        Log.d(TAG, "rollbackAction: 还原ActionBar");
        mActionBar.setTitle(null);
//        mActionBar.setIcon(null);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            rollbackAction();
        } else {
            initActionBar();
        }
    }

    public static boolean isIntercept() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(HIApplication.getContext());
        boolean result = preferences.getBoolean(HARASSMENT_INTERCEPTION_SWITCH, true);
        return result;
    }

    public static boolean isRecord(){

        return true;
    }
}
